package com.cdshop.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class NavigatorService {

	protected NavigatorService() {
	}

	protected static NavigatorService singleton = new NavigatorService();

	public static NavigatorService instance() {
		return singleton;
	}

	public void forward(ServletContext context, HttpServletRequest request,
			HttpServletResponse response, String path) throws ServletException,
			IOException {
		RequestDispatcher disp = context.getRequestDispatcher(path);
		disp.forward(request, response);
	}

	public void forwardToName(ServletContext context,
			HttpServletRequest request, HttpServletResponse response,
			String name) throws ServletException, IOException {
		RequestDispatcher disp = context.getNamedDispatcher(name);
		disp.forward(request, response);
	}

	public void forward(HttpServletRequest request,
			HttpServletResponse response, String path) throws ServletException,
			IOException {

		RequestDispatcher disp = request.getRequestDispatcher(path);
		disp.forward(request, response);
	}

	public void redirect(HttpServletRequest request,
			HttpServletResponse response, String path) throws IOException {
		response.sendRedirect(path);
	}

	public void setCaptureURL(HttpServletRequest request) {
		request.getSession().setAttribute(RequestAttribute.CAPTURED_URL,
				request.getRequestURL());
	}

	public StringBuffer getCaptureURL(HttpServletRequest request) {
		return (StringBuffer) request.getSession().getAttribute(
				RequestAttribute.CAPTURED_URL);
	}
}
